package com.jwb.filter;

import com.alibaba.fastjson.JSONObject;
import com.jwb.controller.SecureLoginController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
@Slf4j
public class CodeFilter extends OncePerRequestFilter implements InitializingBean {

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Autowired
    private SecureLoginController secureLoginController;

    @Autowired
    private RedisTemplate redisTemplate;

    private static  String url = "/**/authentication/form";
    //  https://www.cnblogs.com/quartz/p/13256904.html
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String t = request.getRequestURI();
        if (antPathMatcher.match(url,request.getRequestURI())&& "POST".equals(request.getMethod())){
            try {
                JSONObject result = JSONObject.parseObject(secureLoginController.compareCode(request));
                if(!"0".equals(result.getString("respCode"))){
                    throw new RuntimeException(result.getString("respDesc"));
                }
                Cookie[] cookies = request.getCookies();
                String uuid = "";
                for (Cookie cookie : cookies)
                {
                    String cookieName = cookie.getName();
                    if ("captcha".equals(cookieName))
                    {
                        uuid = cookie.getValue();
                    }
                }
                redisTemplate.delete(uuid);
            }catch (Exception e){
                log.error(e.getMessage());
                HttpSession session = request.getSession();
                session.setAttribute("username",request.getParameter("username"));
                session.setAttribute("error",e.getMessage());
                response.sendRedirect("../index");
                return;
            }
            //            try{
//                String code = request.getParameter("code");
//                String checkCode = (String) request.getSession().getAttribute("checkCode");
//                if(StringUtils.isEmpty(checkCode)||StringUtils.isEmpty(code)){
//                    log.error("验证码不能为空");
//                    throw new RuntimeException("验证码不能为空");
//                }else if(!checkCode.equals(code)){
//                    log.error("验证码不匹配: "+checkCode+"!="+code);
//                    throw new RuntimeException("验证码错误");
//                }
//            }catch (Exception e){
//                HttpSession session = request.getSession();
//                session.setAttribute("username",request.getParameter("username"));
//                session.setAttribute("error",e.getMessage());
//                response.sendRedirect("../index");
//                return;
//            }
        }
        filterChain.doFilter(request,response);
    }


}
